package com.example.demo2.Util;

import com.alibaba.excel.EasyExcel;
import com.example.demo2.generator.entity.UserMessage;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
@Component
public class ExcelUtil {
    public  static <T> void getExcel(Class<T> Class, List<T> list,String sheetName,HttpServletResponse response) throws IOException {
//        String fileName = "F:/Java/excel/"+sheetName + System.currentTimeMillis() + ".xlsx";//userinfo+当前时间戳
        EasyExcel.write(response.getOutputStream(), Class)
                .registerConverter(new LocalDateConverter())
                .sheet(sheetName).doWrite(list);
    }

    /**
     * 设置响应头
     * @param response
     * @param message
     * @throws UnsupportedEncodingException
     */
    public static void inputExcel(HttpServletResponse response, String message) throws UnsupportedEncodingException {
        response.setContentType("application/octet-stream;charset=UTF-8");//设置编码格式
        response.setCharacterEncoding("UTF-8");//设置字符集，防止中文乱码
        String fileName= URLEncoder.encode("Java/excel/"+message+System.currentTimeMillis()+".xlsx","UTF-8");//防止中文乱码
        response.setHeader("Content-disposition","attachment;filename="+fileName);//设置响应头
    }
    public static void read(){

    }

}
